﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using T.ServiceFramework.Spi;

namespace T.FAS.Runtime.ScheduleJob.TSFPlugin
{
    public class ScheduleJobServiceProvider : T.ServiceFramework.Spi.IServiceProvider, T.ServiceFramework.Spi.IAutoRunService
    {
        public List<string> ClusterTypeList { get; private set; }
        public List<LocalServicePackage> Packages { get; private set; }

        public ServiceType ServiceType => ServiceType.Udf;

        public event RestartEventHandler OnRestart;
        public event LogPostEventHandler OnLogPost;
        public event ServiceNotWorkEventHandler OnServiceNotWork;
        public event ServicePackageLoadedEventHandler OnServiceLoaded;


        public ScheduleJobServiceProvider()
        {
        }

        public ScheduleJobServiceProvider(List<string> clusterTypeList, List<LocalServicePackage> packages)
        {
            SetConfig(clusterTypeList, packages);
        }

        private void SetConfig(List<string> clusterTypeList, List<LocalServicePackage> packages)
        {
            this.ClusterTypeList = clusterTypeList;
            if (packages == null)
                this.Packages = packages;
            else
                this.Packages = packages.Where(x => x.ServiceType == this.ServiceType || x.ServiceScope == ServiceScope.Global || x.ServiceScope == ServiceScope.SubSystem).ToList();
        }

        public List<string> GetLoadedServices()
        {
            return new List<string>();
        }

        public List<ServiceMetadata> GetServiceList()
        {
            return new List<ServiceMetadata>();
        }

        public HealthInfo HealthCheck()
        {
            return null;
        }

        public void Init(ServiceProviderConfig config)
        {

        }

        public void RemoveService(string serviceMetadataID)
        {

        }

        public void Reset(List<LocalServicePackage> packages, List<string> clusterTypeList)
        {

        }

        public void Start(Binding binding)
        {
            StartInternal();
        }
        public void Stop()
        {

        }

        public void Suspend(List<ServiceMetadata> services)
        {

        }

        public void Start(IDictionary<string, string> configData)
        {
            StartInternal();
        }
        private void StartInternal()
        {
            try
            {
                Object p;
                var handle = Activator.CreateInstance("T.FAS.Runtime.ScheduleJob.SGPlugin", "T.FAS.Runtime.ScheduleJob.SGPlugin.ScheduleJobSGService", true, BindingFlags.CreateInstance, null, null, null, null);
                p = handle.Unwrap();
                Type t = p.GetType();
                MethodInfo method = t.GetMethod("ResumeAllJobs");

                method.Invoke(p, new object[] { });
            }
            catch (Exception e)
            {

            }
        }
    }
}
